/**
 * 
 */
package com.hk.Mgr.web.controller.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.department.OrganizationReq;
import com.hk.Mgr.web.message.department.OrganizationRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.entity.Organization;
import com.hk.service.IOrganization;

/**
 * @author psx 部门实现类
 */
@Controller
@RequestMapping(value = BaseCtrl.CommonRootPath + "/ComOrganizationCtrl")
public class ComOrganizationCtrl extends BaseCtrl {

	@Autowired
	private IOrganization OrganizationService;

	/**
	 * @Title: getOrganizationList
	 * @Description: 分页获取系统模块
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/getOrganizationList", method = RequestMethod.POST)
	public ResponseBase<List<OrganizationRes>> getOrganizationList(
			@RequestBody OrganizationReq req) {
		ResponseBase<List<OrganizationRes>> resp = new ResponseBase<List<OrganizationRes>>();
		HqlFilter hqlFilter = new HqlFilter();
		/*
		if (StringUtils.isNotEmpty(req.getName())) {
			hqlFilter.addEQFilter("name", req.getName());
		}
		hqlFilter.addSort("id");
		hqlFilter.addOrder("desc");
		*/
		try {
			List<Organization> OrganizationList = OrganizationService.findByFilter(
					hqlFilter);

			List<OrganizationRes> respList = new ArrayList<OrganizationRes>();
			// 根据模块id查询资源
			if (OrganizationList != null && !OrganizationList.isEmpty()) {
				OrganizationRes singleResp = null;
				for (Organization Organization : OrganizationList) {
					//singleResp = new OrganizationRes();
					ReqToModelHelper
							.copyReqValueToModel(Organization, singleResp);
					respList.add(singleResp);
				}
			}
			Long total = OrganizationService.countByFilter(hqlFilter);
			resp.setSize(total);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setResult(respList);

		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}
	
	
}
